<template>
	<view>
		<uni-popup ref="popup" type="bottom" background-color="#fff">
			<view class="popup_title">选择商品</view>
			<view class="searbox">
				<image :src="BASE_IMG_URL+'/search.png'" class="searchimg" mode=""></image>
				<input type="text" placeholder="输入商品编号/名称搜索" @input="queryList" class="selfinput">
			</view>
			<scroll-view scroll-y class="scrollbox"  lower-threshold="30" @scrolltolower="loodMore">
				<view class="pop_list">
					<view class="pop_li" v-for="(item,index) in list" :key="index">
						<view class="pop_li_top">
							<view class="pop_title">{{item.name}}</view>
						</view>
						<view class="pop_info" @tap.stop="changeSelect(index)">
							<view class="pop_radio">
								<radio color='#008EFF' :checked="item.select" ></radio>
							</view>
							<image :src="item.img" class="infoimg" mode=""></image>
							<view class="pop_info_cen">
								<!-- <view class="pop_info_text">商品名称：{{item.goods?item.goods.name:''}}</view> -->
								<view class="pop_info_text">单位：{{item.unit}}</view>
								<view class="pop_info_text">零售价：￥{{item.price}}</view>
								<view class="pop_info_text">批发价：{{item.wholesale?item.wholesale:0}}</view>
								<view class="pop_bottom" @tap.stop.prevent v-if="showNumber">
									数量
									<view class="conright">
										<uni-number-box v-if="type == 1" :min="1" v-model="item.number" @change="(e)=>{changeNumber(e,index)}"></uni-number-box>
										<uni-number-box v-if="type != 1" :min="1" :max="item.maxnumber" v-model="item.number" @change="(e)=>{changeNumber(e,index)}"></uni-number-box>
									</view>
								</view>
							</view>
						</view>
					</view>
					<u-empty v-if="list.length == 0" text="暂无更多"></u-empty>
				</view>
			</scroll-view>
			<view class="popup_bottom">
				<view class="pp_left_box">
					<view class="pp_left">
						<radio color="#ff7800" :checked="isAll" @click="selectAll"></radio>全选
					</view>
					<view class="selectnum">已选{{selectArr.length}}项</view>
				</view>
				<view class="pp_right">
					<view class="right_btn remove" @click="closePopup">取消</view>
					<view class="right_btn sure" @click="sureSelectData">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { netSaleslipProduct, netGoodsList } from '@/api/api.js'
	import { BASE_IMG_URL } from '@/api/http.js'
	
	export default{
		props:{
			showProduct:{
				type:Boolean,
				default:false
			},
			showNumber:{
				type:Boolean,
				default:true
			}
		},
		data(){
			return{
				BASE_IMG_URL:BASE_IMG_URL,
				name:'',
				list:[],
				page:1,
				totalPage:1,
				isAll:false,
				selectArr:[],
				type:1,  //1从产品列表选择   2从合同的产品选择
				contract_id:''
			}
		},
		methods:{
			queryList(e) {
				this.name = e.detail.value
				this.list = []
				if(this.type == 2){
					//从合同 获取产品
					this.getContractProduct()
				}else{
					this.getProductList()
				}
			},
			init(obj) {
				this.$refs.popup.open()
				this.list = []
				if(obj){
					this.type = obj.type
					this.contract_id = obj.contract_id
					this.getContractProduct()
				}else{
					this.getProductList()
				}
			},
			loodMore() {
				if(this.page < this.totalPage){
					this.page ++
					this.getProductList()
				}
			},
			getProductList() {
				let params = {
					name:this.name,
					page:this.page
				}
				netGoodsList(params).then(res=>{
					let arr = res.data.data
					arr.forEach(ele=>{
						ele.number = 1
						ele.select = false
					})
					this.list = this.list.concat(arr)
					this.totalPage = res.data.last_page
				})
			},
			getContractProduct() {
				netSaleslipProduct({contract_id:this.contract_id}).then(res=>{
					let arr = res.data
					arr.forEach(ele=>{
						ele.maxnumber = ele.number
						ele.number = 1
						ele.select = false
					})
					this.list = arr
				})
			},
			changeNumber(e,index) {
				let obj = this.list[index]
				if(e > 1){
					obj.select = true
				}
				obj.number = e
				this.$set(this.list,index,obj)
				this.handleSelect()
			},
			//选择
			changeSelect(index) {
				let obj = this.list[index]
				obj.select = !obj.select
				this.$set(this.list,index,obj)
				this.handleSelect()
			},
			//全选
			selectAll() {
				this.isAll = !this.isAll
				let arr = this.list
				if(this.isAll){
					arr.forEach(ele=>{
						ele.select = true
					})
				}else{
					arr.forEach(ele=>{
						ele.select = false
					})
				}
				this.list = arr
				this.handleSelect()
			},
			closePopup() {
				this.$refs.popup.close()
			},
			handleSelect() {
				let arr = this.list
				let newdata = []
				arr.forEach(ele=>{
					if(ele.select){
						newdata.push(ele.id)
					}
				})
				this.selectArr = newdata
			},
			//确定
			sureSelectData() {
				let arr = this.list
				let newarr = []
				arr.forEach(ele=>{
					if(ele.select){
						newarr.push(ele)
					}
				})
				this.closePopup()
				this.$emit('selectList',newarr)
			}
		}
	}
</script>

<style lang="scss" scoped>
	view{
		box-sizing: border-box;
	}
	.popup_title{
		text-align: center;
		font-size:34rpx;
		color:#333333;
		padding:24rpx;
	}
	.searbox{
		width:650rpx;
		height:68rpx;
		border-radius: 34rpx;
		margin:0 auto;
		border:1rpx solid $uni-text-color;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding:0 15rpx;
		.searchimg{
			width:48rpx;
			height:48rpx;
			margin-right:15rpx;
		}
		.selfinput{
			width:450rpx;
			font-size:26rpx;
			color:$uni-text-color;
		}
	}
	.scrollbox{
		width:100%;
		height:700rpx;
		.pop_list{
			padding:24rpx;
			.pop_li{
				background:#fff;
				box-shadow: 1rpx 1rpx 8rpx 2rpx rgba(0,0,0,0.1);
				border-radius: 24rpx;
				margin-bottom:24rpx;
				.pop_li_top{
					padding:28rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom:1rpx solid #f5f5f5;
					.pop_title{
						font-size:34rpx;
						color:#333333;
					}
					.pop_num{
						font-size:30rpx;
						color:#666666;
					}
				}
				.pop_info{
					display: flex;
					justify-content: flex-start;
					align-items: center;
					padding:24rpx 30rpx;
					.pop_radio{
						margin-right:32rpx;
						radio{
							transform: scale(0.7);
						}
					}
					.infoimg{
						width:140rpx;
						height:140rpx;
						border-radius: 20rpx;
						margin-right:60rpx;
					}
					.pop_info_cen{
						.pop_info_text{
							font-size:28rpx;
							color:#666666;
							margin-bottom:15rpx;
						}
						.pop_bottom{
							display: flex;
							justify-content: flex-start;
							align-items: center;
							font-size:28rpx;
							color:#666666;
							.conright{
								margin-left:10rpx;
								.stepbtn{
									width:38rpx;
									height:38rpx;
									border:1rpx solid $uni-text-color;
								}
							}
						}
					}
				}
			}
		}
	}
	.popup_bottom{
		width:100%;
		padding:0 24rpx;
		height:100rpx;
		border-top:1rpx solid #f5f5f5;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.pp_left_box{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.pp_left{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				font-size:26rpx;
				color:3333;
				radio{
					transform: scale(0.7);
				}
			}
			.selectnum{
				font-size:30rpx;
				color:#FE9440;
				margin-left:24rpx;
			}
		}
		
		.pp_right{
			display: flex;
			justify-content: flex-end;
			align-items: center;
			.right_btn{
				width:165rpx;
				height:60rpx;
				font-size:30rpx;
				text-align: center;
				border-radius: 30rpx;
			}
			.remove{
				border:1rpx solid $uni-text-color;
				color:$uni-text-color;
				line-height: 58rpx;
				margin-right:24rpx;
			}
			.sure{
				background:$uni-text-color;
				color:#fff;
				line-height: 58rpx;
			}
		}
	}
</style>
